<script setup>
import {h, onMounted, ref, resolveDirective} from 'vue'
import {NAlert, NAvatar, NDataTable, NFlex, NInput, NSelect, NTag} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudTable from '@/components/table/CrudTable.vue'

import { useCRUD } from '@/composables'
import api from '@/api'
import default_image from "@/assets/images/mz.jpg";

defineOptions({ name: '操作日志' })

const $table = ref(null)
const queryItems = ref({})

const {
  modalVisible,
  modalTitle,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '操作日志',
  initForm: {
    args: {
      ping_sleep: 1,
      ping_threshold: 500,
      agent_data_check_threshold: 2,
      agent_incoming_traffic_threshold: 30,
      before_start_clean_sleep: 2,
      before_stop_disposal_sleep: 20,
    },
  },
  doCreate: api.createHostMonitor,
  doUpdate: api.updateHostMonitor,
  doDelete: api.deleteHostMonitor,
  refresh: () => $table.value?.handlerefresh(),
})

onMounted(() => {
  $table.value?.handleSearch()
})

function formatTimestamp(timestamp) {
  const date = new Date(timestamp)

  const pad = (num) => num.toString().padStart(2, '0')

  const year = date.getFullYear()
  const month = pad(date.getMonth() + 1) // 月份从0开始，所以需要+1
  const day = pad(date.getDate())
  const hours = pad(date.getHours())
  const minutes = pad(date.getMinutes())
  const seconds = pad(date.getSeconds())

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

// 获取当天的开始时间的时间戳
function getStartOfDayTimestamp() {
  const now = new Date()
  now.setHours(0, 0, 0, 0) // 将小时、分钟、秒和毫秒都设置为0
  return now.getTime()
}

// 获取当天的结束时间的时间戳
function getEndOfDayTimestamp() {
  const now = new Date()
  now.setHours(23, 59, 59, 999) // 将小时设置为23，分钟设置为59，秒设置为59，毫秒设置为999
  return now.getTime()
}

// 示例使用
const startOfDayTimestamp = getStartOfDayTimestamp()
const endOfDayTimestamp = getEndOfDayTimestamp()

const datetimeRange = ref([startOfDayTimestamp, endOfDayTimestamp])
const handleDateRangeChange = (value) => {
  queryItems.value.start_time = formatTimestamp(value[0])
  queryItems.value.end_time = formatTimestamp(value[1])
}

const methodOptions = [
  {
    label: 'GET',
    value: 'GET',
  },
  {
    label: 'POST',
    value: 'POST',
  },
  {
    label: 'DELETE',
    value: 'DELETE',
  },
]

const columns = [
    {
    title: '+',
    width: 60,
    key: 'sku',
    align: 'center',
    type: 'expand',
    expandable: row => row.content !== null,
    renderExpand: (row) => {
       return h(
        NAlert,
        { type: 'warning',show_icon:"false", style: { whiteSpace: 'normal' } },
        { default: () => JSON.stringify(row.content) }
      )

     }
  },
  {
    title: '表名',
    key: 'table',
    width: 'auto',
    align: 'center',
    ellipsis: { tooltip: true },
  },
  {
    title: '目标id',
    key: 'target_id',
    align: 'center',
    width: 'auto',
    ellipsis: { tooltip: true },
  },
  {
    title: '行为',
    key: 'action',
    align: 'center',
    width: 'auto',
    render(row) {
      return h(NTag, { type: 'primary', bordered:false, }, { default: () => row.action })
    },
  },
  {
    title: '请求方法',
    key: 'method',
    align: 'center',
    width: 'auto',
    ellipsis: { tooltip: true },
  },

  {
    title: '操作人',
    key: 'username',
    align: 'center',
    width: 'auto',
    ellipsis: { tooltip: true },
  },
  {
    title: '操作时间',
    key: 'created_at',
    align: 'center',
    width: 'auto',
    ellipsis: { tooltip: true },
  },
]
</script>

<template>
  <!-- 业务页面 -->
  <CommonPage>

    <!-- 表格 -->
    <CrudTable
      ref="$table"
      v-model:query-items="queryItems"
      :columns="columns"
      :get-data="api.getAuditLogList"
      :maxheight="360"
    >
      <template #queryBar>
        <QueryBarItem label="用户名称" :label-width="70">
          <NInput
            v-model:value="queryItems.username"
            clearable
            type="text"
            placeholder="请输入用户名称"
            @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>
        <QueryBarItem label="关键词" :label-width="70">
          <NInput
            v-model:value="queryItems.content"
            clearable
            type="text"
            placeholder="请输入关键词"
            @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>

        <QueryBarItem label="创建时间" :label-width="70">
          <NDatePicker
            v-model:value="queryItems.time"
            type="datetimerange"
            clearable
            placeholder="请选择时间范围"
            @update:value="handleDateRangeChange"
          />
        </QueryBarItem>
      </template>
    </CrudTable>
  </CommonPage>
</template>

